package com.wo.android.pockybill;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnDragListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.wow.wa.bill.serivce.BillService;
import com.wow.wa.bill.serivce.BillServiceImpl;
import com.wow.wa.bill.util.Args;
import com.wow.wa.bill.vo.Card;

/**
 * 卡片修改
 * 
 * @author Andy
 * 
 */
public class EditCardActivty extends BaseActivity {

	private static final String TAG = "EditCardActivty";
	private ListView cardListView;
	private BillService service;
	private List<Card> cards;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Args.Common.EDIT_CARD_CODE) {
			final List<String> bills = new ArrayList<String>();
			cards = this.service.getCard(this);
			for (Card card : cards) {
				bills.add(card.getName());
			}

			ListAdapter adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, bills);
			this.cardListView.setAdapter(adapter);
		}

		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public Class<? extends BaseActivity> toWhenError() {
		// TODO Auto-generated method stub
		return (Class<? extends BaseActivity>) com.wo.android.pockybill.util.Args.Activity.homeActivity;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.edit_card_view);
		this.getActionBar().setDisplayHomeAsUpEnabled(true);

		Log.v(TAG, "onCreate ");

		this.service = new BillServiceImpl(this);

		this.cardListView = (ListView) this.findViewById(R.id.card_list);
		this.cardListView.setOnDragListener(new OnDragListener() {

			@Override
			public boolean onDrag(View v, DragEvent event) {

				event.getX();

				// TODO Auto-generated method stub
				return false;
			}
		});

		final List<String> bills = new ArrayList<String>();
		cards = this.service.getCard(this);
		for (Card card : cards) {
			bills.add(card.getName());
		}

		ListAdapter adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, bills);
		this.cardListView.setAdapter(adapter);
		this.cardListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent it = new Intent(EditCardActivty.this,
						EditDetialActivity.class);
				it.putExtra(Args.DBSchema.CARD_TABLE_NAME, cards.get(arg2));

				EditCardActivty.this.startActivityForResult(it,
						Args.Common.EDIT_CARD_CODE);

			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:
			this.finish();
			break;

		default:
			break;
		}
		return true;
	}
}
